using BSDS.Core.Model;
using BSDS.Core.Model.Data;

namespace BSDS.Core.Data.Local.Csv.Parsing
{
    public class CsvAttributeInfoParser
    {
        //TODO: refactor 
        public AttributeInfo FromCsv(string value)
        {
            string name, datatype;
            bool isPrimaryKey;

            string[] parts = value.Split('(');
            name = parts[0];

            string typeInfo = parts[1].Substring(0, parts[1].Length - 1);

            if (!typeInfo.Contains("@") && !typeInfo.Contains(","))
            {
                datatype = typeInfo;
                isPrimaryKey = false;
            }
            else
            {
                if (typeInfo.Contains("@"))
                    datatype = typeInfo.Split('@')[0];
                else
                    datatype = typeInfo.Split(',')[0];
                isPrimaryKey = true;
            }

            datatype = datatype.Split('.')[1];

            return new AttributeInfo(name, ConvertDatatype(datatype), isPrimaryKey);
        }

        private static string ConvertDatatype(string datatype)
        {
            if (datatype.ToLower() == "int64")
            {
                return "long";
            }

            return datatype;
        }
    }
}